package com.ruoyi.jjyh.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.jjyh.domain.JjyhGoodsrelease;
import com.ruoyi.jjyh.domain.JjyhPayrecord;
import com.ruoyi.jjyh.domain.JjyhReport;
import com.ruoyi.jjyh.domain.JjyhUser;
import com.ruoyi.jjyh.service.IJjyhGoodsreleaseService;
import com.ruoyi.jjyh.service.IJjyhPayrecordService;
import com.ruoyi.jjyh.service.IJjyhUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.*;

/**
 * 活动Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/jjyh/report")
public class JjyhReportController extends BaseController
{
    private String prefix = "jjyh/report";

    @Autowired
    private IJjyhUserService jjyhUserService;
    @Autowired
    private IJjyhPayrecordService jjyhPayrecordService;
    @Autowired
    private IJjyhGoodsreleaseService jjyhGoodsreleaseService;

    @RequiresPermissions("jjyh:report:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/report";
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("whzj:report:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String yearSearch, String monthSearch) throws ParseException {
        //startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
        List<JjyhReport> list = new ArrayList<>();
        //获取系统日期
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        if(EmptyUtils.isNotEmpty(yearSearch) && EmptyUtils.isNotEmpty(monthSearch)){
            year = Integer.parseInt(yearSearch);
            month = Integer.parseInt(monthSearch)-1;
        }
        List<String> dateList = getDayByMonth(year,month);
        int begin = pageSize*(pageNum-1);
        int end = pageSize*pageNum;
        for(int i=begin;i<dateList.size();i++){
            if(i == end){
                break;
            }
            //获取当天用户量
            JjyhUser jjyhUser = new JjyhUser();
            jjyhUser.setDateStr(dateList.get(i));
            JjyhReport temp = new JjyhReport();
            temp.setUserCount(jjyhUserService.selectJjyhUserList(jjyhUser).size());
            temp.setReportDate(dateList.get(i));

            //获取当天充值总金额
            JjyhPayrecord jjyhPayrecord =  new JjyhPayrecord();
            jjyhPayrecord.setPayType("购买会员");
            jjyhPayrecord.setDateStr(dateList.get(i));
            float packageSum = 0;
            List<JjyhPayrecord> jjyhPayrecordList = jjyhPayrecordService.selectJjyhPayrecordList(jjyhPayrecord);
            for(int k=0;k<jjyhPayrecordList.size();k++){
                packageSum += jjyhPayrecordList.get(k).getPayMoney().floatValue();
            }
            temp.setRechargeSum(packageSum);
            //当天上架商品数量
            JjyhGoodsrelease jjyhGoodsrelease = new JjyhGoodsrelease();
            jjyhGoodsrelease.setGoodsStatus("上架");
            jjyhGoodsrelease.setDateStr(dateList.get(i));
            int goodsCount =0;
            List<JjyhGoodsrelease> jjyhGoodsreleaseList = jjyhGoodsreleaseService.selectJjyhGoodsreleaseList(jjyhGoodsrelease);
            temp.setGoodsCount(jjyhGoodsreleaseList.size());
            list.add(temp);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(dateList.size());
        return rspData;
    }


    /**
     * 查询汇总
     */
    @RequiresPermissions("whzj:report:list")
    @PostMapping("/getCount")
    @ResponseBody
    public AjaxResult getCount(String yearSearch, String monthSearch) throws ParseException {
        Map<String,Object> resultMap = new HashMap<>();
        //统计总充值金额
        JjyhPayrecord jjyhPayrecord =new JjyhPayrecord();
        List<JjyhPayrecord> jjyhPayrecordList = jjyhPayrecordService.selectJjyhPayrecordList(jjyhPayrecord);
        float packageSum = 0;
        if(jjyhPayrecordList.size()>0){
            for(JjyhPayrecord temp:jjyhPayrecordList){
                packageSum +=temp.getPayMoney().floatValue();
            }
        }
        resultMap.put("packageSum",packageSum);
        //统计当月充值金额
        float rechargeSum =0;
        float withdrawSum =0;
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        if(EmptyUtils.isNotEmpty(yearSearch) && !"undefined".equals(yearSearch) && !"undefined".equals(monthSearch)  && EmptyUtils.isNotEmpty(monthSearch)){
            year = Integer.parseInt(yearSearch);
            month = Integer.parseInt(monthSearch)-1;
        }
        List<String> dateList = getDayByMonth(year,month);
        for(int i=0;i<dateList.size();i++){
            //获取当天充值总金额
            JjyhPayrecord jjyhPayrecord1 =  new JjyhPayrecord();
            jjyhPayrecord1.setDateStr(dateList.get(i));
            float dpackageSum = 0;
            List<JjyhPayrecord> jjyhPayrecordList1 = jjyhPayrecordService.selectJjyhPayrecordList(jjyhPayrecord1);
            for(int k=0;k<jjyhPayrecordList1.size();k++){
                dpackageSum += jjyhPayrecordList1.get(k).getPayMoney().floatValue();
            }
            rechargeSum +=dpackageSum;
        }
        //获取用户总数
        resultMap.put("userCount",jjyhUserService.selectJjyhUserList(new JjyhUser()).size());
        resultMap.put("rechargeSum",rechargeSum);
        return  new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultMap);
    }

    public static List<String> getDayByMonth(int yearParam,int monthParam){
        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,monthParam,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }
}
